package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.DBHelper;
import cn.wolfcode.wolf2w.util.RedisKeys;
import cn.wolfcode.wolf2w.vo.StrategyStatisVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

import static cn.wolfcode.wolf2w.domain.Strategy.ABROAD_NO;
import static cn.wolfcode.wolf2w.domain.Strategy.ABROAD_YES;

/**
* 攻略服务接口实现
*/
@Service
//@Transactional  现在写不能事务, 需要讲完复制集才能使用事务
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyRepository repsitory;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private StringRedisTemplate redisTemplate;



    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);
        repsitory.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        repsitory.save(strategy);
    }

    @Override
    public void delete(String id) {
        repsitory.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        return repsitory.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> list() {
        return repsitory.findAll();
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage()- 1,qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template, Strategy.class, query, pageable);
    }

    @Override
    public List<Strategy> queryByCatalogId(String catalogId) {
        return repsitory.findByCatalogId(catalogId);
    }

    @Override
    public List<Strategy> queryByDestIdOrderByViewnum(String destId) {
        Query query = new Query();
        if (StringUtils.hasLength(destId)){
            query.addCriteria(Criteria.where("destId").is(destId));
        }
        Pageable pageable = PageRequest.of(0,3, Sort.Direction.DESC, "viewnum");
        query.with(pageable);
        List<Strategy> strategies = template.find(query, Strategy.class);
        return strategies;
    }

    @Override
    public Page<Strategy> queryByDestId(StrategyQuery qo) {

        Query query = new Query();
        // 如果 destId 不为空，作为条件查询
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if (StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()- 1,qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template, Strategy.class, query, pageable);
    }

    @Override
    public void updateState(Strategy strategy) {
        AssertUtil.hasLength(strategy.getId(),"攻略id为空");
        // 查询
        Strategy strategy1 = this.get(strategy.getId());
        // 编辑
        strategy1.setState(strategy.getState());
        // 修改
        repsitory.save(strategy1);
    }

    @Override
    public void updateStatisByVo(StrategyStatisVO vo) {
        Query query = new Query();
        if (StringUtils.hasLength(vo.getStrategyId())){
            query.addCriteria(Criteria.where("id").is(vo.getStrategyId()));
        }
        Update update = new Update();
        update.set("viewnum",vo.getViewnum());
        update.set("replynum",vo.getReplynum());
        update.set("favornum",vo.getFavornum());
        update.set("sharenum",vo.getSharenum());
        update.set("thumbsupnum",vo.getThumbsupnum());
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> queryByDestName(String destName) {

        return repsitory.findByDestName(destName);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        // destId
        // destName
        StrategyCatalog catalog = strategyCatalogService.get(strategy.getCatalogId());
        strategy.setDestId(catalog.getDestId());
        strategy.setDestName(catalog.getDestName());
        // isabroad 是否是国外  需要获取到目的地对象，然后判断这个目的地对象的父类名字 是不是 中国
        Destination dest = destinationService.get(catalog.getDestId());
        if ("中国".equals(dest.getParentName())){
            strategy.setIsabroad(ABROAD_NO);
        }else {
            strategy.setIsabroad(ABROAD_YES);
        }
        // catalogName      分类名称
        strategy.setCatalogName(catalog.getName());
        // themeName       主题名称
        StrategyTheme theme = strategyThemeService.get(strategy.getThemeId());
        strategy.setThemeName(theme.getName());
        if(StringUtils.hasLength(strategy.getId())){
            Strategy strategyOld = this.get(strategy.getId());
            // viewnum 阅读
            strategy.setViewnum(strategyOld.getViewnum());
            // replynum 回复
            strategy.setReplynum(strategyOld.getReplynum());
            // favornum 收藏
            strategy.setFavornum(strategyOld.getFavornum());
            // sharenum 分享
            strategy.setSharenum(strategyOld.getSharenum());
            // thumbsupnum 点赞
            strategy.setThumbsupnum(strategyOld.getThumbsupnum());
            //update
            this.update(strategy);
        }else{
            // 创建时间
            strategy.setCreateTime(new Date());
            //save
            this.save(strategy);
        }
    }
}
